package com.unreal.resourcemanager.service;

import com.unreal.resourcemanager.pojo.ResourceVo;
import com.unreal.resourcemanager.utils.Circulate;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: unreal
 * @Date: 2021-03-20 15:55
 * @Description:
 */
@Service
public class ManagerService {

    public List<ResourceVo> loadFiles(String path){
        List<ResourceVo> list = new ArrayList<>();
        File rootPath = new File(path);
        if(rootPath.isDirectory()){
            File[] files = rootPath.listFiles();
            for (File file: files){
                ResourceVo resourceVo = new ResourceVo();
                resourceVo.setFileName(file.getName());
                resourceVo.setDirectory(file.isDirectory());
                resourceVo.setFilePath(file.getAbsolutePath());
                resourceVo.setParentFilePath(file.getParentFile().getAbsolutePath());
                resourceVo.setLastModifiedDate(new Date(file.lastModified()));
                resourceVo.setLength(file.length());
                resourceVo.setFileSize(Circulate.getFileSize(file.length()));
                list.add(resourceVo);
            }
        }
        return list;
    }
}
